package com.facebook.imagepipeline.core;

import android.net.Uri;
import bolts.Continuation;
import bolts.Task;
import com.android.internal.util.Predicate;
import com.facebook.cache.common.CacheKey;
import com.facebook.common.internal.Objects;
import com.facebook.common.internal.Objects.ToStringHelper;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Supplier;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.util.UriUtil;
import com.facebook.datasource.DataSource;
import com.facebook.datasource.DataSources;
import com.facebook.datasource.SimpleDataSource;
import com.facebook.imagepipeline.cache.BufferedDiskCache;
import com.facebook.imagepipeline.cache.CacheKeyFactory;
import com.facebook.imagepipeline.cache.MemoryCache;
import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.datasource.CloseableProducerToDataSourceAdapter;
import com.facebook.imagepipeline.datasource.ProducerToDataSourceAdapter;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.listener.ForwardingRequestListener;
import com.facebook.imagepipeline.listener.RequestListener;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imagepipeline.producers.Producer;
import com.facebook.imagepipeline.producers.SettableProducerContext;
import com.facebook.imagepipeline.producers.ThreadHandoffProducerQueue;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequest.RequestLevel;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class ImagePipeline
{
  private static final CancellationException PREFETCH_EXCEPTION = new CancellationException("Prefetching is not enabled");
  private final MemoryCache<CacheKey, CloseableImage> mBitmapMemoryCache;
  private final CacheKeyFactory mCacheKeyFactory;
  private final MemoryCache<CacheKey, PooledByteBuffer> mEncodedMemoryCache;
  private AtomicLong mIdCounter = new AtomicLong();
  private final Supplier<Boolean> mIsPrefetchEnabledSupplier;
  private final BufferedDiskCache mMainBufferedDiskCache;
  private final ProducerSequenceFactory mProducerSequenceFactory;
  private final RequestListener mRequestListener;
  private final BufferedDiskCache mSmallImageBufferedDiskCache;
  private final ThreadHandoffProducerQueue mThreadHandoffProducerQueue;

  public ImagePipeline(ProducerSequenceFactory paramProducerSequenceFactory, Set<RequestListener> paramSet, Supplier<Boolean> paramSupplier, MemoryCache<CacheKey, CloseableImage> paramMemoryCache, MemoryCache<CacheKey, PooledByteBuffer> paramMemoryCache1, BufferedDiskCache paramBufferedDiskCache1, BufferedDiskCache paramBufferedDiskCache2, CacheKeyFactory paramCacheKeyFactory, ThreadHandoffProducerQueue paramThreadHandoffProducerQueue)
  {
    this.mProducerSequenceFactory = paramProducerSequenceFactory;
    this.mRequestListener = new ForwardingRequestListener(paramSet);
    this.mIsPrefetchEnabledSupplier = paramSupplier;
    this.mBitmapMemoryCache = paramMemoryCache;
    this.mEncodedMemoryCache = paramMemoryCache1;
    this.mMainBufferedDiskCache = paramBufferedDiskCache1;
    this.mSmallImageBufferedDiskCache = paramBufferedDiskCache2;
    this.mCacheKeyFactory = paramCacheKeyFactory;
    this.mThreadHandoffProducerQueue = paramThreadHandoffProducerQueue;
  }

  private String generateUniqueFutureId()
  {
    return String.valueOf(this.mIdCounter.getAndIncrement());
  }

  private Predicate<CacheKey> predicateForUri(final Uri paramUri)
  {
    return new Predicate()
    {
      public boolean apply(CacheKey paramAnonymousCacheKey)
      {
        return paramAnonymousCacheKey.containsUri(paramUri);
      }
    };
  }

  private <T> DataSource<CloseableReference<T>> submitFetchRequest(Producer<CloseableReference<T>> paramProducer, ImageRequest paramImageRequest, ImageRequest.RequestLevel paramRequestLevel, Object paramObject)
  {
    while (true)
    {
      try
      {
        ImageRequest.RequestLevel localRequestLevel = ImageRequest.RequestLevel.getMax(paramImageRequest.getLowestPermittedRequestLevel(), paramRequestLevel);
        String str = generateUniqueFutureId();
        RequestListener localRequestListener = this.mRequestListener;
        if (!paramImageRequest.getProgressiveRenderingEnabled())
        {
          boolean bool2 = UriUtil.isNetworkUri(paramImageRequest.getSourceUri());
          bool1 = false;
          if (bool2)
          {
            DataSource localDataSource = CloseableProducerToDataSourceAdapter.create(paramProducer, new SettableProducerContext(paramImageRequest, str, localRequestListener, paramObject, localRequestLevel, false, bool1, paramImageRequest.getPriority()), this.mRequestListener);
            return localDataSource;
          }
        }
      }
      catch (Exception localException)
      {
        return DataSources.immediateFailedDataSource(localException);
      }
      boolean bool1 = true;
    }
  }

  private DataSource<Void> submitPrefetchRequest(Producer<Void> paramProducer, ImageRequest paramImageRequest, ImageRequest.RequestLevel paramRequestLevel, Object paramObject, Priority paramPriority)
  {
    try
    {
      ImageRequest.RequestLevel localRequestLevel = ImageRequest.RequestLevel.getMax(paramImageRequest.getLowestPermittedRequestLevel(), paramRequestLevel);
      DataSource localDataSource = ProducerToDataSourceAdapter.create(paramProducer, new SettableProducerContext(paramImageRequest, generateUniqueFutureId(), this.mRequestListener, paramObject, localRequestLevel, true, false, paramPriority), this.mRequestListener);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public void clearCaches()
  {
    clearMemoryCaches();
    clearDiskCaches();
  }

  public void clearDiskCaches()
  {
    this.mMainBufferedDiskCache.clearAll();
    this.mSmallImageBufferedDiskCache.clearAll();
  }

  public void clearMemoryCaches()
  {
    Predicate local3 = new Predicate()
    {
      public boolean apply(CacheKey paramAnonymousCacheKey)
      {
        return true;
      }
    };
    this.mBitmapMemoryCache.removeAll(local3);
    this.mEncodedMemoryCache.removeAll(local3);
  }

  public void evictFromCache(Uri paramUri)
  {
    evictFromMemoryCache(paramUri);
    evictFromDiskCache(paramUri);
  }

  public void evictFromDiskCache(Uri paramUri)
  {
    evictFromDiskCache(ImageRequest.fromUri(paramUri));
  }

  public void evictFromDiskCache(ImageRequest paramImageRequest)
  {
    CacheKey localCacheKey = this.mCacheKeyFactory.getEncodedCacheKey(paramImageRequest, null);
    this.mMainBufferedDiskCache.remove(localCacheKey);
    this.mSmallImageBufferedDiskCache.remove(localCacheKey);
  }

  public void evictFromMemoryCache(Uri paramUri)
  {
    Predicate localPredicate = predicateForUri(paramUri);
    this.mBitmapMemoryCache.removeAll(localPredicate);
    this.mEncodedMemoryCache.removeAll(localPredicate);
  }

  public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(ImageRequest paramImageRequest, Object paramObject)
  {
    try
    {
      DataSource localDataSource = submitFetchRequest(this.mProducerSequenceFactory.getDecodedImageProducerSequence(paramImageRequest), paramImageRequest, ImageRequest.RequestLevel.FULL_FETCH, paramObject);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public DataSource<CloseableReference<PooledByteBuffer>> fetchEncodedImage(ImageRequest paramImageRequest, Object paramObject)
  {
    Preconditions.checkNotNull(paramImageRequest.getSourceUri());
    try
    {
      Producer localProducer = this.mProducerSequenceFactory.getEncodedImageProducerSequence(paramImageRequest);
      if (paramImageRequest.getResizeOptions() != null)
        paramImageRequest = ImageRequestBuilder.fromRequest(paramImageRequest).setResizeOptions(null).build();
      DataSource localDataSource = submitFetchRequest(localProducer, paramImageRequest, ImageRequest.RequestLevel.FULL_FETCH, paramObject);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public DataSource<CloseableReference<CloseableImage>> fetchImageFromBitmapCache(ImageRequest paramImageRequest, Object paramObject)
  {
    try
    {
      DataSource localDataSource = submitFetchRequest(this.mProducerSequenceFactory.getDecodedImageProducerSequence(paramImageRequest), paramImageRequest, ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE, paramObject);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public MemoryCache<CacheKey, CloseableImage> getBitmapMemoryCache()
  {
    return this.mBitmapMemoryCache;
  }

  public CacheKeyFactory getCacheKeyFactory()
  {
    return this.mCacheKeyFactory;
  }

  public Supplier<DataSource<CloseableReference<CloseableImage>>> getDataSourceSupplier(final ImageRequest paramImageRequest, final Object paramObject, final boolean paramBoolean)
  {
    return new Supplier()
    {
      public DataSource<CloseableReference<CloseableImage>> get()
      {
        if (paramBoolean)
          return ImagePipeline.this.fetchImageFromBitmapCache(paramImageRequest, paramObject);
        return ImagePipeline.this.fetchDecodedImage(paramImageRequest, paramObject);
      }

      public String toString()
      {
        return Objects.toStringHelper(this).add("uri", paramImageRequest.getSourceUri()).toString();
      }
    };
  }

  public Supplier<DataSource<CloseableReference<PooledByteBuffer>>> getEncodedImageDataSourceSupplier(final ImageRequest paramImageRequest, final Object paramObject)
  {
    return new Supplier()
    {
      public DataSource<CloseableReference<PooledByteBuffer>> get()
      {
        return ImagePipeline.this.fetchEncodedImage(paramImageRequest, paramObject);
      }

      public String toString()
      {
        return Objects.toStringHelper(this).add("uri", paramImageRequest.getSourceUri()).toString();
      }
    };
  }

  public boolean isInBitmapMemoryCache(Uri paramUri)
  {
    if (paramUri == null)
      return false;
    Predicate localPredicate = predicateForUri(paramUri);
    return this.mBitmapMemoryCache.contains(localPredicate);
  }

  public boolean isInBitmapMemoryCache(ImageRequest paramImageRequest)
  {
    if (paramImageRequest == null)
      return false;
    CacheKey localCacheKey = this.mCacheKeyFactory.getBitmapCacheKey(paramImageRequest, null);
    CloseableReference localCloseableReference = this.mBitmapMemoryCache.get(localCacheKey);
    try
    {
      boolean bool = CloseableReference.isValid(localCloseableReference);
      return bool;
    }
    finally
    {
      CloseableReference.closeSafely(localCloseableReference);
    }
  }

  public DataSource<Boolean> isInDiskCache(Uri paramUri)
  {
    return isInDiskCache(ImageRequest.fromUri(paramUri));
  }

  public DataSource<Boolean> isInDiskCache(ImageRequest paramImageRequest)
  {
    final CacheKey localCacheKey = this.mCacheKeyFactory.getEncodedCacheKey(paramImageRequest, null);
    final SimpleDataSource localSimpleDataSource = SimpleDataSource.create();
    this.mMainBufferedDiskCache.contains(localCacheKey).continueWithTask(new Continuation()
    {
      public Task<Boolean> then(Task<Boolean> paramAnonymousTask)
        throws Exception
      {
        if ((!paramAnonymousTask.isCancelled()) && (!paramAnonymousTask.isFaulted()) && (((Boolean)paramAnonymousTask.getResult()).booleanValue()))
          return Task.forResult(Boolean.valueOf(true));
        return ImagePipeline.this.mSmallImageBufferedDiskCache.contains(localCacheKey);
      }
    }).continueWith(new Continuation()
    {
      public Void then(Task<Boolean> paramAnonymousTask)
        throws Exception
      {
        SimpleDataSource localSimpleDataSource = localSimpleDataSource;
        if ((!paramAnonymousTask.isCancelled()) && (!paramAnonymousTask.isFaulted()) && (((Boolean)paramAnonymousTask.getResult()).booleanValue()));
        for (boolean bool = true; ; bool = false)
        {
          localSimpleDataSource.setResult(Boolean.valueOf(bool));
          return null;
        }
      }
    });
    return localSimpleDataSource;
  }

  public boolean isInDiskCacheSync(Uri paramUri)
  {
    return isInDiskCacheSync(ImageRequest.fromUri(paramUri));
  }

  public boolean isInDiskCacheSync(ImageRequest paramImageRequest)
  {
    CacheKey localCacheKey = this.mCacheKeyFactory.getEncodedCacheKey(paramImageRequest, null);
    return this.mMainBufferedDiskCache.diskCheckSync(localCacheKey);
  }

  public boolean isPaused()
  {
    return this.mThreadHandoffProducerQueue.isQueueing();
  }

  public void pause()
  {
    this.mThreadHandoffProducerQueue.startQueueing();
  }

  public DataSource<Void> prefetchToBitmapCache(ImageRequest paramImageRequest, Object paramObject)
  {
    if (!((Boolean)this.mIsPrefetchEnabledSupplier.get()).booleanValue())
      return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION);
    try
    {
      DataSource localDataSource = submitPrefetchRequest(this.mProducerSequenceFactory.getDecodedImagePrefetchProducerSequence(paramImageRequest), paramImageRequest, ImageRequest.RequestLevel.FULL_FETCH, paramObject, Priority.MEDIUM);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public DataSource<Void> prefetchToDiskCache(ImageRequest paramImageRequest, Object paramObject)
  {
    return prefetchToDiskCache(paramImageRequest, paramObject, Priority.MEDIUM);
  }

  public DataSource<Void> prefetchToDiskCache(ImageRequest paramImageRequest, Object paramObject, Priority paramPriority)
  {
    if (!((Boolean)this.mIsPrefetchEnabledSupplier.get()).booleanValue())
      return DataSources.immediateFailedDataSource(PREFETCH_EXCEPTION);
    try
    {
      DataSource localDataSource = submitPrefetchRequest(this.mProducerSequenceFactory.getEncodedImagePrefetchProducerSequence(paramImageRequest), paramImageRequest, ImageRequest.RequestLevel.FULL_FETCH, paramObject, paramPriority);
      return localDataSource;
    }
    catch (Exception localException)
    {
      return DataSources.immediateFailedDataSource(localException);
    }
  }

  public void resume()
  {
    this.mThreadHandoffProducerQueue.stopQueuing();
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.core.ImagePipeline
 * JD-Core Version:    0.6.2
 */